#include "bsp_ws2812.h"
extern	TIM_HandleTypeDef htim3;
uint16_t static RGB_buffur[RESET_PULSE + WS2812_DATA_LEN] = { 0 };
void ws2812_set_RGB(uint8_t R, uint8_t G, uint8_t B, uint16_t num)
{
	//指针偏移:需要跳过复位信号的N个0
	uint16_t* p = (RGB_buffur + RESET_PULSE) + (num * LED_DATA_LEN);
    
	for (uint16_t i = 0;i < 8;i++)
	{
		//填充数组
		p[i]      = (G << i) & (0x80) ? ONE_PULSE : ZERO_PULSE;
		p[i + 8]  = (R << i) & (0x80) ? ONE_PULSE : ZERO_PULSE;
		p[i + 16] = (B << i) & (0x80) ? ONE_PULSE : ZERO_PULSE;
	}

}

void ws2812_example(void)
{
	//#1.填充数组
	  for(uint16_t j = 0 ; j < 4 ; j++)
	{
		for (uint16_t i = 0; i < 8; i++)
		{
			switch (j) {
			case 0:
				ws2812_set_RGB(0xff, 0x00, 0x00, i);
				break;
			case 1:
				ws2812_set_RGB(0x00, 0xff, 0x00, i);
				break;
			case 2:
				ws2812_set_RGB(0x00, 0x00, 0xff, i);
				break;
			case 3:
			default:
				ws2812_set_RGB(0xff, 0xff, 0xff, i);
				break;
			}
				
		}
		//#2.传输数据
		HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_3, (uint32_t *)RGB_buffur, (176 * 2));
		HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_4, (uint32_t *)RGB_buffur, (176 * 2));  
		//#3.延时：使效果可以被观察
		HAL_Delay(1000);
	}
}